-- [[ uncomment it to disable this module ]]--
-- do return end

g_Tree = Proto("testTree", "testTreeDescriptor")

print("Script testTree")

-- define protocol fields
-- local f1 = ProtoField.new("testTree.BOOLEAN", "BOOLEAN", ftypes.BOOLEAN)
-- local pf_bool = ProtoField.bool("testTree.BOOLEAN", "BOOLEAN", base.NONE)--, base.HEX, {'True', 'False'}, 0xF0, 'blob?')

local pf_bool = ProtoField.bool("testTree.BOOLEAN", "BOOLEAN", base.NONE)
local pf_char = ProtoField.char("testTree.CHAR", "CHAR")
local pf_int8 = ProtoField.int8("testTree.INT8", "INT8")
local pf_int16 = ProtoField.int16("testTree.INT16", "INT16")
local pf_int24 = ProtoField.int24("testTree.INT24", "INT24")
local pf_int32 = ProtoField.int32("testTree.INT32", "INT32")
local pf_int64 = ProtoField.int64("testTree.INT64", "INT64")
local pf_uint8 = ProtoField.uint8("testTree.UINT8", "UINT8")
local pf_uint16 = ProtoField.uint16("testTree.UINT16", "UINT16")
local pf_uint24 = ProtoField.uint24("testTree.UINT24", "UINT24")
local pf_uint32 = ProtoField.uint32("testTree.UINT32", "UINT32")
local pf_uint64 = ProtoField.uint64("testTree.UINT64", "UINT64")

local pf_float = ProtoField.float("testTree.FLOAT", "FLOAT")
local pf_double = ProtoField.double("testTree.DOUBLE", "DOUBLE")
local pf_string = ProtoField.string("testTree.STRING", "STRING")
local pf_stringz = ProtoField.stringz("testTree.STRINGZ", "STRINGZ")
local pf_bytes = ProtoField.bytes("testTree.BYTES", "BYTES")


g_Tree.fields = {
                pf_bool,  pf_char, 
                pf_int8,  pf_int16,  pf_int24,  pf_int32,   pf_int64,
                pf_uint8, pf_uint16, pf_uint24, pf_uint32,  pf_uint64, 
                pf_float,  pf_double, pf_string, pf_stringz, pf_bytes
                }

-- define dissector function (Tvb, Pinfo, TreeItem)
function g_Tree.dissector(buf, pkt, tree)
    print('which one called me?', pkt.proto_name)
    len=0;
    local pktlen = buf:reported_length_remaining()
    print(type(buf), buf, pktlen)
    print(type(tree), tree)

    local subtree = tree:add(g_Tree, buf:range(0, pktlen)) 
    print(type(subtree), subtree)
    subtree:add(pf_bool, buf:range(len, 1)):append_text(" | 0 = Master | 1 = Slave")
    len = len + 1
    subtree:add(pf_char, buf:range(len, 1))
    len = len + 1
    if pktlen >= (len + 4) then
        subtree:add(pf_uint32, buf:range(len, 4))
        len = len + 4
    end
    do return end

    -- subtree:add(pf_origin , buf:range(len,1))--:append_text(" | 0 = Master | 1 = Slave")
    -- len = len + 1
    -- subtree:add(pf_triggerSignal, buf:range(len,1))--:append_text(" | 0= None | 1= Start | 2= Ready | 3= Done")
    -- len = len + 1
    -- subtree:add(pf_syncSlaveId, buf:range(len,1))
    -- len = len + 1
    -- subtree:add(pf_valueStampType, buf:range(len,1))--:append_text(" | 0= Invalid | 1= timestamp | 2= delta offset")
    -- len = len + 1
    -- subtree:add(pf_maxOffsetIter, buf:range(len,1))
    -- len = len + 1
    -- subtree:add(pf_reqOffsetIter, buf:range(len,1))
    -- len = len + 1
    -- subtree:add(pf_reserved, buf:range(len,1))
    -- len = len + 2
    -- if endianess == 1 then
        -- subtree:add(pf_value, buf:range(len,8))
    -- else
        -- subtree:add_le(pf_value, buf:range(len,8))
    -- end
    -- len = len + 8
end

print("Script testTree exit")
